//
//  PhoneNumAuthenticationController.m
//  ShanYanSDk
//
//  Created by 白红光 on 2018/7/5.
//  Copyright © 2018年 白红光. All rights reserved.
//

#import "PhoneNumAuthenticationController.h"

#import "OrdinaryLoginController.h"
#import <CL_ShanYanSDK/CLSecretLoginAndMachineCheckManager.h>
#import "ViewController.h"

@interface PhoneNumAuthenticationController ()
{
    MBProgressHUD *mHUD;
}

@property (nonatomic, strong) CLSecretLoginAndMachineCheckManager *secretLoginAndMach;
@property(nonatomic,weak) UITextField * iphoneField;
@end

@implementation PhoneNumAuthenticationController

- (CLSecretLoginAndMachineCheckManager *)secretLoginAndMach {
    
    if (_secretLoginAndMach == nil) {
        _secretLoginAndMach = [[CLSecretLoginAndMachineCheckManager alloc] init];
    }
    return _secretLoginAndMach;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:false];
    self.navigationController.navigationBar.hidden = NO;
    
    [self backBtn];
    
}

- (void)backBtn {
    
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [backButton setImage:[UIImage imageNamed:@"title_back.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backItemClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    
}

- (void)backItemClick {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self hideByProgress];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"手机号认证";
    
    [self setupUI];
    
    if (self.str.length > 0) {
        [self showMessage:self.str];
    }
}

- (void)authenticationBtnClick {
    [self hintByProgress:@"请稍后..."];

    [self.secretLoginAndMach getAllIdAndKeyWithCLAppid:@"" withCLAppKey:@"" withCLNav:self.navigationController withCLController:self withRequestType:@"bjjy" withIphoneNum:self.iphoneField.text];
    
    __weak typeof(self) weakself = self;
    
    self.secretLoginAndMach.resultBlock = ^(NSDictionary *resultStr){
        
        NSLog(@"验证2：passedValue:%@",resultStr);
        
        NSString * code = resultStr[@"code"];
        if ([code isEqualToString:@"1100"]) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                for (UIViewController *controller in weakself.navigationController.viewControllers) {
                    
                    if ([controller isKindOfClass:[ViewController class]]) {
                        
                        ViewController *vc = (ViewController *)controller;
                        vc.result = weakself.iphoneField.text;
                        [weakself.navigationController popToViewController:vc animated:YES];
                        
                    }
                }
            });
        }else if ([code isEqualToString:@"1106"]){
            
            OrdinaryLoginController * ordLoginVC = [OrdinaryLoginController new];
            ordLoginVC.str = @"当前网络不稳定，已为您切换至验证码登录页面";
            ordLoginVC.iphoneNum = weakself.iphoneField.text;
            [weakself.navigationController pushViewController:ordLoginVC animated:YES];
            
        }else if ([code isEqualToString:@"1108"]){
            
            [weakself hideByProgress];
            [weakself showMessage:resultStr[@"msg"]];

        }
    };
}

- (void)setupUI {
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.modalPresentationCapturesStatusBarAppearance = NO;
    
    UIImageView * topImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"矢量智能对象"]];
    [self.view addSubview:topImg];
    [topImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.offset(70*WIDTHRADIU);
        make.width.offset(56*WIDTHRADIU);
        make.height.offset(28*WIDTHRADIU);
        
    }];
    
    UITextField * iphoneField = [[UITextField alloc] init];
    iphoneField.placeholder = @"请输入手机号";
    iphoneField.font = [UIFont systemFontOfSize:15.f*WIDTHRADIU];
    [self.view addSubview:iphoneField];
    [iphoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(37*WIDTHRADIU);
        make.right.offset(-32*WIDTHRADIU);
        make.top.equalTo(topImg.mas_bottom).offset(85*WIDTHRADIU);
        
    }];
    self.iphoneField = iphoneField;
    
    UIView * firstLine = [[UIView alloc] init];
    firstLine.backgroundColor = RGBCOLOR(205,208,212);
    [self.view addSubview:firstLine];
    [firstLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(iphoneField.mas_bottom).offset(13*WIDTHRADIU);
        make.left.offset(37*WIDTHRADIU);
        make.right.offset(-38*WIDTHRADIU);
        make.height.offset(1*WIDTHRADIU);
        
    }];
    
    // 登录
    UIButton * authenticationBtn = [UIButton new];
    [authenticationBtn setTitle:@"认证" forState:UIControlStateNormal];
    [authenticationBtn setTintColor:[UIColor whiteColor]];
    [authenticationBtn.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
    [authenticationBtn setBackgroundColor:RGBCOLOR(78,132,255)];
    [self.view addSubview:authenticationBtn];
    [authenticationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(firstLine.mas_bottom).offset(20*WIDTHRADIU);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.offset(300*WIDTHRADIU);
        make.height.offset(50*WIDTHRADIU);
        
    }];
    [authenticationBtn addTarget:self action:@selector(authenticationBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [authenticationBtn.layer setMasksToBounds:YES];
    [authenticationBtn.layer setCornerRadius:3.0];
    
}

// 吐丝
- (void)showMessage:(NSString *)msg{
    
    if ([msg isEqualToString:@""]) {
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = msg;
    hud.detailsLabel.font = [UIFont systemFontOfSize:18.f];
    hud.detailsLabel.backgroundColor = [UIColor clearColor];
    hud.margin = 15;
    hud.backgroundView.alpha = 0.8;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:2.0];
    
}


- (void) hintByProgress:(NSString*) text{
    
    mHUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:mHUD];
    mHUD.label.text = text;
    mHUD.label.textColor = [UIColor whiteColor];
    mHUD.contentColor = [UIColor blackColor];
    mHUD.removeFromSuperViewOnHide = YES;
    mHUD.animationType = MBProgressHUDAnimationZoom;
    [mHUD showAnimated:YES];
    
}

/**
 *  隐藏认证提示框
 */
- (void) hideByProgress{
    if(mHUD){
        [mHUD hideAnimated:YES];
        mHUD = nil;
    }
}

@end
